#include "database.h"
#include "QVector"
#include "qvariant.h"
#include <map>
#include <iterator>
#include <QDebug>
#include <qsqlerror.h>
DataBase::DataBase()
{
    host = "localhost";
    name = "customDb";
    status = false;
}

bool DataBase::setSettings(QString _host, QString _dbName)
{
    host = _host;
    name = _dbName;

    return true;
}

bool DataBase::connectWithDataBase(QString login, QString password)
{
    db = QSqlDatabase::addDatabase("QMYSQL", name);
    db.setHostName(host);
    db.setDatabaseName(name);
    db.setUserName(login);
    db.setPassword(password);
    status = db.open();

//    qDrbug() << "!!!" << db.lastError().text();

    return status;
}
bool DataBase::isOpen()
{
    return status;
}

void DataBase::disconnectWithDataBase()
{
    db.close();
    db.removeDatabase("customdb");
    status = db.isOpen();
}

QSqlQuery* DataBase::getResultFor(QString queStr)
{
    query = new QSqlQuery(db);
    bool result = query->exec(queStr);
    if(result == false)
        status = false;

    return query;
}
QSqlQuery* DataBase::getClientInfo(QString currPrefix)
{
    query = new QSqlQuery(db);
    QString queStr;

    queStr = "SELECT * FROM client WHERE prefix = '";
    queStr += currPrefix + "'";
    bool result = query->exec(queStr);

    if(result == false)
        status = false;

    return query;
}

QString DataBase::getCurrentPrefix(QString currentOrder)
{
    query = new QSqlQuery(db);
    QString queStr;

    queStr = "SELECT PREFIX FROM REQUEST WHERE REQUEST_ID = '";
    queStr += currentOrder + "'";
    bool result = query->exec(queStr);

    if(result == false)
        status = false;

    query->next();
    return (QString)query->value("PREFIX").toString();
}

QSqlQuery* DataBase::getClientInfo()
{
    query = new QSqlQuery(db);
    QString queStr;
    queStr = "SELECT * FROM client";
    bool result = query->exec(queStr);

    if(result == false)
        status = false;

    return query;
}

QSqlQuery *DataBase::getOrderInfo()
{
    query = new QSqlQuery(db);
    QString queStr;
    queStr = "SELECT * FROM REQUEST";
    bool result = query->exec(queStr);

    if(result == false)
        status = false;

    return query;
}

QSqlQuery *DataBase::getDeclInfo(QString orderId)
{
    query = new QSqlQuery(db);
    QString queStr;
    queStr = "SELECT * FROM DECLORATION WHERE REQUEST_ID = '";
    queStr += orderId + "'";
    bool result = query->exec(queStr);

    if(result == false)
        status = false;

    return query;
}

QSqlQuery *DataBase::getContactInfo(QString prefix)
{
    query = new QSqlQuery(db);
    QString queStr;
    queStr = "SELECT * FROM contacts WHERE prefix = '";
    queStr += prefix + "'";
    bool result = query->exec(queStr);
    qDebug() << query->lastError().text();

    if(result == false)
        status = false;

    return query;
}

QSqlQuery *DataBase::getDepartInfo(QString prefix)
{
    query = new QSqlQuery(db);
    QString queStr;
    queStr = "SELECT * FROM preTs WHERE DECLORATION_ID = '";
    queStr += prefix + "'";
    bool result = query->exec(queStr);
    qDebug() << query->lastError().text();

    if(result == false)
        status = false;

    return query;
}

QSqlQuery *DataBase::getBorderInfo(QString prefix)
{
    query = new QSqlQuery(db);
    QString queStr;
    queStr = "SELECT * FROM preBorder WHERE DECLORATION_ID = '";
    queStr += prefix + "'";
    bool result = query->exec(queStr);
    qDebug() << query->lastError().text();

    if(result == false)
        status = false;

    return query;
}

QSqlQuery *DataBase::getTransInfo(QString prefix)
{
    query = new QSqlQuery(db);
    QString queStr;
    queStr = "SELECT * FROM transCosts WHERE DECLORATION_ID = '";
    queStr += prefix + "'";
    bool result = query->exec(queStr);
    qDebug() << query->lastError().text();

    if(result == false)
        status = false;

    return query;
}

QSqlQuery *DataBase::getPactsInfo(QString prefix)
{
    query = new QSqlQuery(db);
    QString queStr;
    queStr = "SELECT * FROM pacts WHERE DECLORATION_ID = '";
    queStr += prefix + "'";
    bool result = query->exec(queStr);
    qDebug() << query->lastError().text();

    if(result == false)
        status = false;

    return query;
}

QSqlQuery *DataBase::getOrderInfo(QString currId)
{
    query = new QSqlQuery(db);
    QString queStr;
    queStr = "SELECT * FROM ARTICLE WHERE order_id = '";
    queStr += currId + "'";
    bool result = query->exec(queStr);
    qDebug() << query->lastError().text();

    if(result == false)
        status = false;

    return query;
}

QSqlQuery *DataBase::getCurrentGoodsInfo(QString currId)
{
    query = new QSqlQuery(db);
    QString queStr;
    queStr = "SELECT * FROM ARTICLE WHERE decloration_id = '";
    queStr += currId + "'";
    bool result = query->exec(queStr);
    qDebug() << query->lastError().text();

    if(result == false)
        status = false;

    return query;
}

QSqlQuery *DataBase::getCurrentDeclInfo(QString decl)
{
    query = new QSqlQuery(db);
    QString queStr;
    queStr = "SELECT * FROM DECLORATION WHERE decloration_id = '";
    queStr += decl + "'";
    bool result = query->exec(queStr);
    qDebug() << query->lastError().text();

    if(result == false)
        status = false;

    return query;
}

QSqlQuery* DataBase::addClient(QMap<QString,QString> clientInfo)
{
    query = new QSqlQuery(db);
    QString queStr;
    queStr = "INSERT INTO client (";
    QMapIterator<QString,QString> i(clientInfo);
    while(i.hasNext())
    {

        i.next();
        if(i.hasNext())
            queStr += i.key() + ",";
        else
            queStr += i.key();
    }
    queStr += ") VALUES (";
    i.toFront();
    while(i.hasNext())
    {
        i.next();
        if(i.hasNext())
            queStr += "'" + i.value() + "', ";
        else
            queStr += "'" + i.value() + "'";
    }
    queStr += ")";

    bool result = query->exec(queStr);

    if(result == false)
        status = false;
//    qDebug() << query->lastError().text();

    return query;
}

QSqlQuery *DataBase::addOrder(QMap<QString, QString> orderInfo)
{
    query = new QSqlQuery(db);
    QString queStr;
    queStr = "INSERT INTO REQUEST (";
    QMapIterator<QString,QString> i(orderInfo);
    while(i.hasNext())
    {

        i.next();
        if(i.hasNext())
            queStr += i.key() + ",";
        else
            queStr += i.key();
    }
    queStr += ") VALUES (";
    i.toFront();
    while(i.hasNext())
    {
        i.next();
        if(i.hasNext())
            queStr += "'" + i.value() + "', ";
        else
            queStr += "'" + i.value() + "'";
    }
    queStr += ")";

    bool result = query->exec(queStr);

    if(result == false)
        status = false;

    return query;
}

QString DataBase::addGoods(QMap<QString, QString> goodsInfo)
{
    query = new QSqlQuery(db);
    QString queStr;
    queStr = "INSERT INTO ARTICLE (";
    QMapIterator<QString,QString> i(goodsInfo);
    while(i.hasNext())
    {

        i.next();
        if(i.hasNext())
            queStr += i.key() + ",";
        else
            queStr += i.key();
    }
    queStr += ") VALUES (";
    i.toFront();
    while(i.hasNext())
    {
        i.next();
        if(i.hasNext())
            queStr += "'" + i.value() + "', ";
        else
            queStr += "'" + i.value() + "'";
    }
    queStr += ")";

    bool result = query->exec(queStr);

    if(result == false)
        status = false;

    queStr = "SELECT LAST_INSERT_ID()";
    query->exec(queStr);
    query->next();
    return query->value(0).toString();

}

QString DataBase::addDecloration(QMap<QString, QString> declInfo)
{
    query = new QSqlQuery(db);
    QString queStr;
    queStr = "INSERT INTO DECLORATION (";
    QMapIterator<QString,QString> i(declInfo);
    while(i.hasNext())
    {

        i.next();
        if(i.hasNext())
            queStr += i.key() + ",";
        else
            queStr += i.key();
    }
    queStr += ") VALUES (";
    i.toFront();
    while(i.hasNext())
    {
        i.next();
        if(i.hasNext())
            queStr += "'" + i.value() + "', ";
        else
            queStr += "'" + i.value() + "'";
    }
    queStr += ")";

    bool result = query->exec(queStr);

    if(result == false)
        status = false;

    queStr = "SELECT LAST_INSERT_ID()";
    query->exec(queStr);
    query->next();
    return query->value(0).toString();
}

QString DataBase::updateClient(QMap<QString,QString> clientInfo, QString currPrefix)
{
    query = new QSqlQuery(db);
    QString queStr;
    queStr = "UPDATE client SET ";
    QMapIterator<QString,QString> i(clientInfo);
    while(i.hasNext())
    {
        i.next();
        if(i.hasNext())
            queStr += i.key() + " = '" + i.value() + "'" + ", ";
        else
            queStr += i.key() + " = '" + i.value() + "' ";
    }
    queStr += "WHERE prefix = '";
    queStr += currPrefix + "'";

    bool result = query->exec(queStr);
    qDebug() << query->lastError().text();
    qDebug() << "DataBase::updateClient - result: " << result;
    if(result == false)
        status = false;

    return clientInfo["PREFIX"];
}

QString DataBase::updateGoods(QMap<QString, QString> goodsInfo)
{
    query = new QSqlQuery(db);
    QString queStr;
    queStr = "UPDATE ARTICLE SET ";
    QMapIterator<QString,QString> i(goodsInfo);
    while(i.hasNext())
    {
        i.next();
        if(i.hasNext())
            queStr += i.key() + " = '" + i.value() + "'" + ", ";
        else
            queStr += i.key() + " = '" + i.value() + "' ";
    }
    queStr += "WHERE DECLORATION_ID = '";
    queStr += goodsInfo["DECLORATION_ID"] + "'";

    bool result = query->exec(queStr);
//    qDebug() <<"TEST"<< queStr;
    if(result == false)
        status = false;
    qDebug() << queStr;
    return goodsInfo["DECLORATION_ID"];
}

QString DataBase::updateDecl(QMap<QString, QString> declInfo)
{
    query = new QSqlQuery(db);
    QString queStr;
    queStr = "UPDATE DECLORATION SET ";
    QMapIterator<QString,QString> i(declInfo);
    while(i.hasNext())
    {
        i.next();
        if(i.hasNext())
            queStr += i.key() + " = '" + i.value() + "'" + ", ";
        else
            queStr += i.key() + " = '" + i.value() + "' ";
    }
    queStr += "WHERE DECLORATION_ID = '";
    queStr += declInfo["DECLORATION_ID"] + "'";

    bool result = query->exec(queStr);
//    qDebug() <<"TEST"<< queStr;
    if(result == false)
        status = false;
    qDebug() << queStr;
    qDebug() << query->lastError().text();
    return declInfo["DECLORATION_ID"];
}

QString DataBase::updateGoods(QString cDecl, QString cGoods)
{
    query = new QSqlQuery(db);
    QString queStr;
    queStr = "UPDATE ARTICLE SET DECL = '" + cDecl + "' WHERE DECLORATION_ID = '" + cGoods + "'";
    qDebug () << queStr;

    bool result = query->exec(queStr);
    qDebug () << query->lastError().text();
    if(result == false)
        status = false;
    return cGoods;
}

QSqlQuery* DataBase::updateContact(QMap<QString,QString> contactInfo)
{
    query = new QSqlQuery(db);
    QString queStr;
    queStr = "UPDATE contacts SET ";
    QMapIterator<QString,QString> i(contactInfo);
    while(i.hasNext())
    {
        i.next();
        if(i.hasNext())
            queStr += i.key() + " = '" + i.value() + "'" + ", ";
        else
            queStr += i.key() + " = '" + i.value() + "' ";
    }
    queStr += "WHERE id = '";
    queStr += contactInfo["ID"] + "'";

    bool result = query->exec(queStr);
//    qDebug() <<"TEST"<< queStr;
    if(result == false)
        status = false;
    qDebug() << queStr;
    return query;
}

QSqlQuery *DataBase::updateDepart(QMap<QString, QString> contactInfo)
{
    query = new QSqlQuery(db);
    QString queStr;
    queStr = "UPDATE preTs SET ";
    QMapIterator<QString,QString> i(contactInfo);
    while(i.hasNext())
    {
        i.next();
        if(i.hasNext())
            queStr += i.key() + " = '" + i.value() + "'" + ", ";
        else
            queStr += i.key() + " = '" + i.value() + "' ";
    }
    queStr += "WHERE ID = '";
    queStr += contactInfo["ID"] + "'";

    bool result = query->exec(queStr);
//    qDebug() <<"TEST"<< queStr;
    if(result == false)
        status = false;
    qDebug() << queStr;
    return query;
}

QSqlQuery *DataBase::updateBorder(QMap<QString, QString> contactInfo)
{
    query = new QSqlQuery(db);
    QString queStr;
    queStr = "UPDATE preBorder SET ";
    QMapIterator<QString,QString> i(contactInfo);
    while(i.hasNext())
    {
        i.next();
        if(i.hasNext())
            queStr += i.key() + " = '" + i.value() + "'" + ", ";
        else
            queStr += i.key() + " = '" + i.value() + "' ";
    }
    queStr += "WHERE ID = '";
    queStr += contactInfo["ID"] + "'";

    bool result = query->exec(queStr);
//    qDebug() <<"TEST"<< queStr;
    if(result == false)
        status = false;
    qDebug() << queStr;
    return query;
}

QSqlQuery *DataBase::updateTrans(QMap<QString, QString> contactInfo)
{
    query = new QSqlQuery(db);
    QString queStr;
    queStr = "UPDATE transCosts SET ";
    QMapIterator<QString,QString> i(contactInfo);
    while(i.hasNext())
    {
        i.next();
        if(i.hasNext())
            queStr += i.key() + " = '" + i.value() + "'" + ", ";
        else
            queStr += i.key() + " = '" + i.value() + "' ";
    }
    queStr += "WHERE ID = '";
    queStr += contactInfo["ID"] + "'";

    bool result = query->exec(queStr);
//    qDebug() <<"TEST"<< queStr;
    if(result == false)
        status = false;
    qDebug() << queStr;
    return query;
}

QSqlQuery *DataBase::updatePact(QMap<QString, QString> contactInfo)
{
    query = new QSqlQuery(db);
    QString queStr;
    queStr = "UPDATE pacts SET ";
    QMapIterator<QString,QString> i(contactInfo);
    while(i.hasNext())
    {
        i.next();
        if(i.hasNext())
            queStr += i.key() + " = '" + i.value() + "'" + ", ";
        else
            queStr += i.key() + " = '" + i.value() + "' ";
    }
    queStr += "WHERE ID = '";
    queStr += contactInfo["ID"] + "'";

    bool result = query->exec(queStr);
//    qDebug() <<"TEST"<< queStr;
    if(result == false)
        status = false;
    qDebug() << queStr;
    return query;
}

QString DataBase::addContact(QMap<QString, QString> contactInfo)
{
    query = new QSqlQuery(db);
    QString queStr;
    queStr = "INSERT INTO contacts (";
    QMapIterator<QString,QString> i(contactInfo);
    while(i.hasNext())
    {

        i.next();
        if(i.hasNext())
            queStr += i.key() + ",";
        else
            queStr += i.key();
    }
    queStr += ") VALUES (";
    i.toFront();
    while(i.hasNext())
    {
        i.next();
        if(i.hasNext())
            queStr += "'" + i.value() + "', ";
        else
            queStr += "'" + i.value() + "'";
    }
    queStr += ")";

    bool result = query->exec(queStr);

    if(result == false)
        status = false;

    queStr = "SELECT LAST_INSERT_ID()";
    query->exec(queStr);
    query->next();
    return query->value(0).toString();

}

QString DataBase::addDepart(QMap<QString, QString> contactInfo)
{
    query = new QSqlQuery(db);
    QString queStr;
    queStr = "INSERT INTO preTs (";
    QMapIterator<QString,QString> i(contactInfo);
    while(i.hasNext())
    {

        i.next();
        if(i.hasNext())
            queStr += i.key() + ",";
        else
            queStr += i.key();
    }
    queStr += ") VALUES (";
    i.toFront();
    while(i.hasNext())
    {
        i.next();
        if(i.hasNext())
            queStr += "'" + i.value() + "', ";
        else
            queStr += "'" + i.value() + "'";
    }
    queStr += ")";

    bool result = query->exec(queStr);

    if(result == false)
        status = false;

    queStr = "SELECT LAST_INSERT_ID()";
    query->exec(queStr);
    query->next();
    return query->value(0).toString();
}

QString DataBase::addPact(QMap<QString, QString> contactInfo)
{
    query = new QSqlQuery(db);
    QString queStr;
    queStr = "INSERT INTO pacts (";
    QMapIterator<QString,QString> i(contactInfo);
    while(i.hasNext())
    {

        i.next();
        if(i.hasNext())
            queStr += i.key() + ",";
        else
            queStr += i.key();
    }
    queStr += ") VALUES (";
    i.toFront();
    while(i.hasNext())
    {
        i.next();
        if(i.hasNext())
            queStr += "'" + i.value() + "', ";
        else
            queStr += "'" + i.value() + "'";
    }
    queStr += ")";

    bool result = query->exec(queStr);

    if(result == false)
        status = false;

    queStr = "SELECT LAST_INSERT_ID()";
    query->exec(queStr);
    query->next();
    return query->value(0).toString();
}

QString DataBase::addBorder(QMap<QString, QString> contactInfo)
{
    query = new QSqlQuery(db);
    QString queStr;
    queStr = "INSERT INTO preBorder (";
    QMapIterator<QString,QString> i(contactInfo);
    while(i.hasNext())
    {

        i.next();
        if(i.hasNext())
            queStr += i.key() + ",";
        else
            queStr += i.key();
    }
    queStr += ") VALUES (";
    i.toFront();
    while(i.hasNext())
    {
        i.next();
        if(i.hasNext())
            queStr += "'" + i.value() + "', ";
        else
            queStr += "'" + i.value() + "'";
    }
    queStr += ")";

    bool result = query->exec(queStr);

    if(result == false)
        status = false;

    queStr = "SELECT LAST_INSERT_ID()";
    query->exec(queStr);
    query->next();
    return query->value(0).toString();
}

QString DataBase::addTrans(QMap<QString, QString> contactInfo)
{
    query = new QSqlQuery(db);
    QString queStr;
    queStr = "INSERT INTO transCosts (";
    QMapIterator<QString,QString> i(contactInfo);
    while(i.hasNext())
    {

        i.next();
        if(i.hasNext())
            queStr += i.key() + ",";
        else
            queStr += i.key();
    }
    queStr += ") VALUES (";
    i.toFront();
    while(i.hasNext())
    {
        i.next();
        if(i.hasNext())
            queStr += "'" + i.value() + "', ";
        else
            queStr += "'" + i.value() + "'";
    }
    queStr += ")";

    bool result = query->exec(queStr);

    if(result == false)
        status = false;

    queStr = "SELECT LAST_INSERT_ID()";
    query->exec(queStr);
    query->next();
    return query->value(0).toString();
}

void DataBase::dropGoods(QString currId)
{
    query = new QSqlQuery(db);
    QString queStr;
    queStr = "DELETE FROM ARTICLE WHERE DECLORATION_ID = '";
    queStr+= currId + "'";
    bool result = query->exec(queStr);
    if(result == false)
        status = false;

}
